<?php
namespace app\api\model\foodcash;

use app\common\model\User as UserModel;
use app\api\model\Setting;
use hema\wechat\Pay as WxPay;

/**
 * 会员模型
 */
class User extends UserModel
{
    /**
     * 会员充值
     */
    public function recharge($data)
    {
        $model = new Record;
        $data['mode'] = 20;//后台充值
        $data['order_no'] = order_no();
        $data['remark'] = '收银端充值';
        //现金充值
        if(empty($data['wx_id'])){ 
            $data['type'] = 20; //现金支付
            $this->money = ['inc',$data['money']];
            $this->save();
            $model->add($data);
            return [
                'pay_status' => 20,
                'order_no' => $data['order_no']
            ];
        }
        //微信充值
        $data['type'] = 30; //微信支付

        //获取小程序配置的支付参数
        $payment = Setting::getItem('payment');
        $wxpay = new WxPay($payment['wx']);
        if($transaction_id = $wxpay->unifiedorder($data['order_no'],$data['money'],'micropay','','会员充值',false,$data['wx_id'])){
            //支付成功
            $model->add($data);
            return [
                'pay_status' => 20,
                'order_no' => $data['order_no']
            ];
        }
        $data['status'] = 10;//支付进行中
        $model->add($data);
        //微信支付等待中
        return [
            'pay_status' => 10,
            'order_no' => $data['order_no']
        ];
    }
}
